<template>
  <v-app>
    <v-overlay class="loading_bg text-center" :model-value="true" opacity="0" persistent>
      <v-progress-circular v-show="valid == null" indeterminate size="64" color="blue"></v-progress-circular>
      <v-btn v-if="valid == false" class="pa-12" variant="outlined" size="large" icon color="red" elevation="0">
        <v-icon size="72">mdi-account-multiple-remove-outline</v-icon>
      </v-btn>
      <v-btn v-if="valid == true" class="pa-12" variant="outlined" size="large" icon color="green" elevation="0">
        <v-icon size="72">mdi-account-multiple-check-outline</v-icon>
      </v-btn>
      <div class="mt-6 font-weight-black text-h6 text-black" :class="valid == false ? 'text-red' : ''">{{ note }}</div>
    </v-overlay>
  </v-app>
</template>

<script setup>
import { ref, onMounted, getCurrentInstance } from 'vue'
import { useRouter } from 'vue-router'

const { proxy } = getCurrentInstance()
const router = useRouter()

const valid = ref(null)
const note = ref("授权登录中...")

onMounted(() => {
  const stoken = GetQueryString("stoken")
  if (stoken) {
    UserLoginByData("acis", { stoken: stoken })
    return
  }

  const uid = GetQueryString("uid")
  if (uid) {
    UserLoginByData("frdb", { uid: uid })
    return
  }

  if (!stoken && !uid) {
    valid.value = false
    note.value = "授权登录错误，用户令牌不能为空，请关闭页面后重试！"
  }
})

const GetQueryString = (name) => {
  const reg = new RegExp("[?&]" + name + "=([^&^#^/]*)", "i")
  const res = window.location.href.match(reg)
  return res != null ? decodeURIComponent(res[1]) : ""
}

const UserLoginByData = (type, data) => {
  proxy.$vtk.request.postJson(`badp/admin/login/${type}`, data).then((res) => {
    if (res.meta.success && res.data) {
      valid.value = true
      note.value = "授权登录成功，正在跳转请稍后..."
      const users = res.data.users
      proxy.$vtk.storage.set("user", users)
      proxy.$vtk.storage.set("token", res.data.token)

      setTimeout(() => {
        router.push("/home")
      }, 500)
    } else {
      valid.value = false
      note.value = "授权登录失败，请关闭页面后重试！"
    }
  })
}
</script>

<style scoped>
.loading_bg {
  background-image: url("../../assets/jz.jpg") !important;
  background-size: cover;
}
</style>
